package com.hui.service;

import com.hui.dao.RoleDao;
import com.hui.entity.Menu;
import com.hui.entity.Role;


import java.util.List;

/**
 * @Description Role 业务层
 */
public class RoleService {

    public List<Role> getAllRole() {
        RoleDao dao = new RoleDao();
        List<Role> list = dao.findAll();
        return list;
    }

    public void delRole(int roleId) {
        RoleDao dao = new RoleDao();
        dao.delByRoleId(roleId);
    }

    public Role findByRoleId(int roleId) {
        RoleDao dao = new RoleDao();
        return dao.findByRoleId(roleId);
    }


    public void addRole(Role role, String[] menuIds) {
        RoleDao dao = new RoleDao();
        Integer roleId = dao.addRole(role);

        for (String menuId : menuIds) {
            dao.addRoleMenu(roleId, Integer.parseInt(menuId));
        }
    }

    public List<Menu> findMenuByRoleId(int roleId) {
        RoleDao dao = new RoleDao();
        return dao.findMenuByRoleId(roleId);
    }

    public void updateRole(Role role,String[] menuIds) {
        RoleDao dao = new RoleDao();
        //角色修改方法
        dao.updateRole(role);
        //根据角色id删除和菜单的关联管理
        dao.deleteRoleMenuByRoleId(role.getRoleId());

        //重新添加角色和菜单的关系
        for (String menuId : menuIds) {
            dao.addRoleMenu(role.getRoleId(), Integer.parseInt(menuId));
        }
    }

}
